/**
 * 
 */
package org.gov.vis.master.gui.listener;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.ResourceBundle;

import javax.swing.JOptionPane;

import org.gov.vis.common.exceptions.InvalidInputException;
import org.gov.vis.gui.util.ErrorsDisplayJPanel;
import org.gov.vis.master.controller.StaffCtlr;
import org.gov.vis.master.controller.StaffCtlrImpl;
import org.gov.vis.master.dto.StaffDto;
import org.gov.vis.master.gui.StaffDialog;
import org.gov.vis.util.DBConstants;
import org.gov.vis.util.DU;
import org.gov.vis.util.GuiConstants;
import org.gov.vis.util.Util;

/**
 * @author user
 *
 */
public class AddSaffListener implements ActionListener,BaseListener{


	private StaffDialog staffDailog;
	private StaffCtlr staffCtlr = new StaffCtlrImpl();

	private ErrorsDisplayJPanel errorMsgPanel;
	private StaffDialog addStaffDialog;
	final private ResourceBundle rb = Util.getResources();
	private StaffDto staffDto;

	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */

	public AddSaffListener(StaffDialog staffDialog){

		this.staffDailog = staffDialog;
	}
	@Override
	public void actionPerformed(ActionEvent actionEvent) {
		try{
			errorMsgPanel = new ErrorsDisplayJPanel(staffDailog.getjPanel1());
			errorMsgPanel.clearMsgPanel();

			if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_save"))
					|| actionEvent.getActionCommand().equals(rb.getString("dialog_button_update"))){
				//Method called to either Add or Edit the Staff details
				addOrEditStaff();
			}else if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_view"))){			
				//Method called to view the details of Staff Object based on the passed otherId
				viewStaff(1);
			}else if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_delete"))){
				//Method called to delete the Staff Object based on the passed otherId.
				deleteStaff(1);
			}else if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_reset"))){
				//Method called to clear the screen data
				clear();
			}
		}catch(Exception e){
			e.printStackTrace();
		}

	}//end actionPerformed


	/**
	 * Method called to either Add or Edit the Staff details
	 */
	private void addOrEditStaff(){
		//Method called to set the Field's Labels to default Black color
		clearGUILabelColors(blackColor);

		//Method to set the Screen's data to  the DTO Object
		setGuiDetailsToDto(staffDailog);

		try{
			//Method called to Add or Edit the Staff
			/*staffCtlr.addOrEditStaff(staffDto);
			List<String> successMsgLst = new ArrayList<String>();
			successMsgLst.add(rb.getString(GuiConstants.MASTER_ADD_STAFF_SUCCESS));
			errorMsgPanel.setSuccessMessages(successMsgLst);
			*/
			staffCtlr.addOrEditStaff(staffDto);
			JOptionPane.showMessageDialog(addStaffDialog, rb.getString(GuiConstants.MASTER_ADD_STAFF_SUCCESS));
			//addStaffDialog.dispose();	
			

			//Method called to clear the screen data
			clear();
		}catch(Exception e){
			List<String> errorMsgLst = new ArrayList<String>();
			if(e instanceof InvalidInputException){

				final HashMap<String,String> _exceptions = StaffCtlr.errorMsgs;
				if (_exceptions != null && !_exceptions.isEmpty()) {
					String errMsg = null;

					if(_exceptions.containsKey(rb.getString("dialog_label_staff_code").toUpperCase())){
						staffDailog.getLblCode().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_staff_code").toUpperCase());
						errorMsgLst.add(errMsg);
					}

					if(_exceptions.containsKey(rb.getString("dialog_label_staff_name").toUpperCase())){
						staffDailog.getLblName().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_staff_name").toUpperCase());
						errorMsgLst.add(errMsg);	
					}
					if(_exceptions.containsKey(rb.getString("dialog_label_staff_fathername").toUpperCase())){
						staffDailog.getLblFatherName().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_staff_fathername").toUpperCase());
						errorMsgLst.add(errMsg);
					}

					if(_exceptions.containsKey(rb.getString("dialog_label_staff_dob").toUpperCase())){
						staffDailog.getLblDateOfBirth().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_staff_dob").toUpperCase());
						errorMsgLst.add(errMsg);	
					}
					if(_exceptions.containsKey(rb.getString("dialog_label_staff_dofjoin").toUpperCase())){
						staffDailog.getLblDateOfJoin().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_staff_dofjoin").toUpperCase());
						errorMsgLst.add(errMsg);
					}

					if(_exceptions.containsKey(rb.getString("dialog_label_staff_qualfcation").toUpperCase())){
						staffDailog.getLblQualification().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_staff_qualfcation").toUpperCase());
						errorMsgLst.add(errMsg);	
					}
					if(_exceptions.containsKey(rb.getString("dialog_label_staff_designation").toUpperCase())){
						staffDailog.getLblDesignation().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_staff_designation").toUpperCase());
						errorMsgLst.add(errMsg);
					}

					if(_exceptions.containsKey(rb.getString("dialog_label_staff_emptype").toUpperCase())){
						staffDailog.getLblEmpType().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_staff_emptype").toUpperCase());
						errorMsgLst.add(errMsg);	
					}
					errorMsgPanel.setErrorMessages(errorMsgLst);
				} else {
					//log.debug(e.getMessage(), e);
				}	
			}
			else{
				errorMsgLst.add(e.getMessage());
				errorMsgPanel.setErrorMessages(errorMsgLst);

			}
		}
	}//end addOrEditStaff

	/**
	 * Method called to view the Staff details.
	 */
	private void viewStaff(final long _staffId) {
		// TODO Auto-generated method stub
		try{
			final StaffDto _staffDto = staffCtlr.getStaff(_staffId);
			setDtoDetailsToGui(_staffDto);
			staffDailog.validate();
			staffDailog.repaint();
		}catch(Exception e){
			e.printStackTrace();
		}

	}//end viewStaff

	/**
	 * Method called to delete the Staff Object based on the passed otherId
	 * @param _otherId
	 */
	private void deleteStaff(final long  _staffId){
		try{
			staffCtlr.deleteStaff(_staffId);
		}catch(Exception e){
			e.getMessage();
		}
	}//end deleteStaff


	/**
	 * Method called to Clear all the form fields of the screen
	 */
	@Override
	public void clear() {
		// TODO Auto-generated method stub

		//set the Field's data to empty values
		staffDailog.getTxtFldCode().setText("");
		staffDailog.getDtChoDateOfJoin().setDate(null);
		staffDailog.getTxtFldDistrict().setText("");
		staffDailog.getTxtFldDesignaton().setText("");
		staffDailog.getDtChoDateOfBirth().setDate(null);
		staffDailog.getTxtFldMandal().setText("");
		staffDailog.getTxtFldFatherName().setText("");
		staffDailog.getTxtFldName().setText("");
		staffDailog.getTxtFldQualification().setText("");
		staffDailog.getTxtFldVillage().setText("");
		staffDailog.getCombxEmployeeType().setSelectedIndex(0);

		//Method called to set the Field's Labels to default Black color
		clearGUILabelColors(blackColor);

		//ResultPanel
		//staffDailog.getLblResultOrg().setText("");
		//staffDailog.getLblrRsultCode().setText("");

	}//end clear

	/**
	 * Method called to read the data from GUI and set them to DTO Object 
	 * @param _guiObject
	 */
	@Override
	public void setDtoDetailsToGui(Object object) {
		// TODO Auto-generated method stub
		StaffDto _staffDto = (StaffDto)object;
		staffDailog.getTxtFldCode().setText(_staffDto.getCode());
		staffDailog.getDtChoDateOfJoin().setDateFormatString(DU.format(_staffDto.getDoj(),DBConstants.DISPLAY_DATE_FORMAT));
		staffDailog.getTxtFldDistrict().setText(_staffDto.getDistrict());
		staffDailog.getTxtFldDesignaton().setText(_staffDto.getDesignation());
		staffDailog.getTxtFldDOB().setText(DU.format(_staffDto.getDob(),DBConstants.DISPLAY_DATE_FORMAT));
		staffDailog.getTxtFldMandal().setText(_staffDto.getMandal());
		staffDailog.getTxtFldFatherName().setText(_staffDto.getFatherName());
		staffDailog.getTxtFldName().setText(_staffDto.getName());
		staffDailog.getTxtFldQualification().setText(_staffDto.getQualificatiion());
		staffDailog.getTxtFldVillage().setText(_staffDto.getVillage());
		//	staffDailog.getCombxEmployeeType().setSelectedIndex(0);

	}//end setDtoDetailsToGui

	/**
	 * Method called to read the data from DTO and set them to GUI.
	 * @param _guiObject
	 */
	@Override
	public void setGuiDetailsToDto(Object object) {
		// TODO Auto-generated method stub
		StaffDialog _staffDialog =(StaffDialog)object;
		staffDto = new StaffDto();
		staffDto.setCode(_staffDialog.getTxtFldCode().getText());
		staffDto.setDesignation(_staffDialog.getTxtFldDesignaton().getText());
		staffDto.setDistrict(_staffDialog.getTxtFldDistrict().getText());
		if(_staffDialog.getDtChoDateOfBirth().getDate() != null){
		staffDto.setDob(_staffDialog.getDtChoDateOfBirth().getDate());
		}
		if(_staffDialog.getDtChoDateOfJoin().getDate()!=null){
		staffDto.setDoj(_staffDialog.getDtChoDateOfJoin().getDate());
		}
		staffDto.setEmploymentType(String.valueOf(_staffDialog.getVisComBoxModel().getSelectedKey()));
		staffDto.setFatherName(_staffDialog.getTxtFldFatherName().getText());
		staffDto.setMandal(_staffDialog.getTxtFldMandal().getText());
		staffDto.setName(_staffDialog.getTxtFldName().getText());
		staffDto.setQualificatiion(_staffDialog.getTxtFldQualification().getText());
		staffDto.setVillage(_staffDialog.getTxtFldVillage().getText());

	}//end setGuiDetailsToDto

	public void setStaffDailog(StaffDialog staffDailog) {
		this.staffDailog = staffDailog;
	}

	public StaffDialog getStaffDailog() {
		return staffDailog;
	}

	public void setStaffCtlr(StaffCtlr staffCtlr) {
		this.staffCtlr = staffCtlr;
	}

	public StaffCtlr getStaffCtlr() {
		return staffCtlr;
	}
	@Override
	public void clearGUILabelColors(Color _lblColor) {
		//set the Field's Labels to default Black color
		staffDailog.getLblCode().setForeground(_lblColor);
		staffDailog.getLblDateOfBirth().setForeground(_lblColor);
		staffDailog.getLblDistrict().setForeground(_lblColor);
		staffDailog.getLblDateOfJoin().setForeground(_lblColor);
		staffDailog.getLblDesignation().setForeground(_lblColor);
		staffDailog.getLblMandal().setForeground(_lblColor);
		staffDailog.getLblEmpType().setForeground(_lblColor);
		staffDailog.getLblFatherName().setForeground(_lblColor);
		staffDailog.getLblName().setForeground(_lblColor);
		staffDailog.getLblVillage().setForeground(_lblColor);
		staffDailog.getLblQualification().setForeground(_lblColor);


	}


}
